<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="订单状态">
          <el-select v-model="form.status" placeholder="订单状态">
            <el-option label="待支付" :value="0" />
            <el-option label="成功" :value="10" />
            <el-option label="退款" :value="20" />
            <el-option label="失败" :value="30" />
          </el-select>
        </el-form-item>
        <el-form-item label="订单号">
          <el-input v-model="form.order_no" placeholder="订单号" />
        </el-form-item>
        <el-form-item label="用户uid">
          <el-input v-model="form.uid" placeholder="用户uid"></el-input>
        </el-form-item>
        <el-form-item label="商品ID">
          <el-input v-model="form.goods_id" placeholder="商品id" />
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker v-model="form.timeData" type="datetimerange" range-separator="至" start-placeholder="开始日期"
            end-placeholder="截止日期" :default-time="pickerDefaultTime" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center"  />
          <el-table-column prop="uid" label="用户UID" align="center" width="120" />
          <el-table-column prop="order_no" label="订单号" align="center" width="200" />
          <el-table-column prop="goods_id" label="商品id" align="center"  />
          <el-table-column prop="pay_method" label="支付方式" align="center"  />
          <el-table-column prop="status" label="订单状态" align="center" />
          <el-table-column prop="channel" label="应用渠道" align="center"  />
          <el-table-column prop="diamond" label="购买钻石数" align="center"  />

          <el-table-column prop="quantity" label="下单数" align="center"  />
          <el-table-column prop="base_price" label="价格" align="center" />
          <el-table-column prop="base_amount" label="金额" align="center" />
          <el-table-column prop="base_currency" label="币种" align="center" />
          <el-table-column prop="real_price" label="实际支付价格" align="center" />
          <el-table-column prop="real_amount" label="实际支付金额" align="center" />
          <el-table-column prop="real_currency" label="实际支付币种" align="center" />
          <el-table-column prop="out_order_no" label="渠道订单id" align="center"  />
          <!-- <el-table-column prop="sandbox" label="沙盒环境" align="center"  /> -->

          <!-- <el-table-column
            prop="order_status"
            label="订单状态"
            align="center"
            "90"
            :formatter="formatterOrderStatus"
          />/slotsgame/pay_handle_deposit
           -->
           <el-table-column prop="app_mark" label="app版本" align="center"  />
           <el-table-column prop="createtime" label="下单时间" align="center" width="180" :formatter="formatterTime" />
          <el-table-column prop="expire_time" label="过期时间" align="center" width="180" :formatter="formatterTime" />
          <el-table-column prop="pay_time" label="成功时间" align="center" width="180" :formatter="formatterTime" />
          <el-table-column prop="order_ext" label="扩展信息" align="center"  />
          <el-table-column prop="client_ip" label="ip" align="center"  />
          <el-table-column label="操作" align="center" width="160px"  fixed="right">
            <template #default="scope">
              <!-- <div class="flex"> -->
                <el-button type="primary" plain v-if="scope.row.status == '待支付'" @click="handleOperate(scope.$index, scope.row, 'edit')">入账</el-button>
              <!-- </div> -->
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination :current-page="pageable.pageNum" :page-size="pageable.pageSize" :page-sizes="[10, 25, 50, 100]"
            :background="true" layout="total, sizes, prev, pager, next, jumper" :total="pageable.total"
            @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";

import { formatterTime, timestamp, exportToExcel, pickerDefaultTime } from "@/utils/utils";
import { payOrderListApi ,payHandleDepositApi} from "@/api/slotsgame/order/order";

const tableData = ref([]);

const orderStatus = {
  0: "初始",
  10: "订单已成功",
  20: "订单已退款",
};
const gameStatus = {
  0: "初始",
  10: "通知成功",
  "-10": "通知失败",
};

const formData = {
  order_no: "",
  goods_id: "",
  status: "",
  timeData: [],
};
const form = reactive({ ...formData });
const loading = ref(false);
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

const handleOperate = async (index,row,type) => {
  try {
    const res = await payHandleDepositApi({ order_no: row.order_no });
    // 处理接口返回的结果
    // ...
    console.log('入账成功');
    getTbaleData();
    console.log('111')
  } catch (error) {
    // 处理错误
    // ...
    console.error('入账失败', error);
  }
};

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    goods_id: form.goods_id,
    uid:form.uid,
    order_no: form.order_no,
    status: form.status,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};
//格式化

// const getTbaleData = async () => {
//   loading.value = true;

//   try {
//     const res = await payOrderListApi(param());
//     loading.value = false;
//     tableData.value = res.data.results.map(item => ({
//       ...item,
//       statusText: orderStatus[item.status]
//     }));
//     pageable.total = res.data.count;
//   } catch (error) {
//     loading.value = false;
//   }
// };
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await payOrderListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await payOrderListApi(data);

  let head = {
    uid: "用户id",
    order_no: "订单号",
    goods_id: "商品id",
    channel: "应用渠道",
    pay_method: "支付方式",
    diamond: "购买钻石数",
    quantity: "下单数",
    base_price: "价格",
    base_amount: "金额",
    base_currency: "币种",
    real_price: "实际支付价格",
    real_amount: "实际支付金额",
    real_currency: "实际支付币种",
    out_order_no: "渠道订单id",
    sandbox: "沙盒环境",
    order_ext: "扩展信息",
    ip: "ip",
    expire_time: "过期时间",
    status: "订单状态",
    pay_time: "支付成功时间",
    createtime: "下单时间",
    app_mark: "app版本",
  };
  exportToExcel(res.data.results, head, "Slotsgame-订单列表");
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
